package com.jiantong.lawEnforcing.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.lawEnforcing.impl.dao.GoodsDao;
import com.jiantong.lawEnforcing.impl.manager.DatasManager;
import com.jiantong.lawEnforcing.model.Goods;
import com.jiantong.lawEnforcing.service.GoodsService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 物品信息
 * Created by mz03 on 2017/8/14.
 */
@Slf4j
@Service
@RpcProvider
public class GoodsServiceImpl implements GoodsService {
    private final GoodsDao goodsDao;
    private final DatasManager datasManager;

    @Autowired
    public GoodsServiceImpl(GoodsDao goodsDao, DatasManager datasManager) {
        this.goodsDao = goodsDao;
        this.datasManager = datasManager;
    }


    /**
     * id 查找
     *
     * @param id
     * @return goods
     */
    @Override
    public Response<Goods> finById(Long id) {
        try {
            Goods goods = goodsDao.findById(id);
            if (goods != null) {
                return Response.ok(goods);
            } else {
                log.error("goods (id={}) is not found", id);
                return Response.fail("goods.not.found");
            }
        }catch (Exception e){
            log.error("failed to find goods(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("goods.find.fail");
        }
    }

    /**
     * ids 查找一组符合
     *
     * @param ids
     * @return goods
     */
    @Override
    public Response<List<Goods>> finByIds(List<Long> ids) {
        try {
            List<Goods> goodsList = goodsDao.findByIds(ids);
            if (goodsList != null) {
                return Response.ok(goodsList);
            } else {
                log.error("goodsList (id={}) is not found", ids);
                return Response.fail("goods.not.found");
            }
        }catch (Exception e){
            log.error("failed to find goodsList(ids={}),cause:{}",ids, Throwables.getStackTraceAsString(e));
            return Response.fail("goodsList.find.fail");
        }
    }

    /**
     * 创建
     *
     * @param goods 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(Goods goods) {
        try {
            goodsDao.create(goods);
            return Response.ok(goods.getId());
        }catch (Exception e) {
            log.error("failed.to.create.goods:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.goods.failed");
        }
    }

    /**
     * 创建物品并且更新对应的datas
     *
     * @param goods    物品对象
     * @param lawCase  相关案件id
     * @param writType 文书类型
     * @param idList   data中的物品idList字段
     * @return 物品对象id
     */
    @Override
    public Response<Long> createAndDatas(Goods goods, Long lawCase, Integer writType, String idList) {
        try {
            Long askAnswerId =datasManager.createAndDatas(goods, lawCase, writType, idList);
            return Response.ok(askAnswerId);
        } catch (Exception e) {
            log.error("failed.to.create.goods:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.goods.failed");
        }
    }

    /**
     * 更新新闻的单据
     *
     * @param goods 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(Goods goods) {
        try{
            return Response.ok(goodsDao.update(goods));
        }catch (Exception e){
            log.error("failed.to.update.goods:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("update.goods.failed");
        }
    }
}
